<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.io.IOException"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="com.sci99.ex.model.*"%>
<%@ page import="com.sci99.ex.util.*"%>
<%@ page import="org.apache.log4j.Logger"%>
<%@ page import="org.apache.commons.lang3.math.NumberUtils"%>
<%@ page
	import="org.springframework.security.core.userdetails.UserDetails"%>
<%@ page
	import="org.springframework.security.core.context.SecurityContextHolder"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<%
	Logger log = Logger.getLogger(this.getClass());
	// 当前页
	int currentPage = 1;
	// 每页要显示的记录数
	int lineSize = 10;
	// 总记录数
	int allRecords;
	// 总的页数
	int pageSize;
	int cate = 3;

	// 处理当前页的变化的情况&处理分类变化
	currentPage = NumberUtils.toInt(request.getParameter("cp"), 1);
	cate = NumberUtils.toInt(request.getParameter("cate"), 3);

	Employee user = UserUtil.getUser();

	//log.error(user.getId());
	String sql = "select * from course where id not in (select course_id from employee_course where employee_id = ? ) and category_id = ?";
	List r = new ArrayList();
	Connection con = null;
	ResultSet rs = null;
	try {
		con = JDBCUtil.getCon();
		PreparedStatement ps = con.prepareStatement(sql,
				java.sql.ResultSet.TYPE_FORWARD_ONLY,
				java.sql.ResultSet.CONCUR_READ_ONLY);
		ps.setLong(1, user.getId());
		ps.setLong(2, cate);
		rs = ps.executeQuery();
		while (rs.next()) {
			Map m = new HashMap(8, 1);
			m.put("id", rs.getLong("id"));
			m.put("name", rs.getString("name"));
			m.put("lecturer", rs.getString("lecturer"));
			m.put("period", rs.getString("period"));
			m.put("video_url", VideoUtil.getFlvURL(rs.getString("video_url")));
			r.add(m);
		}
	} catch (SQLException se) {
		log.error("", se);
	} finally {
		try {
			rs.close();
			con.close();
		} catch (Exception inr) {
			log.error("", inr);
		}
	}

	// 得到总的记录数
	allRecords = r.size();
	// 得到总的页数
	if (allRecords % lineSize == 0) {
		pageSize = allRecords / lineSize;
	} else {
		pageSize = allRecords / lineSize + 1;
	}

	// 根据当前页的变化取得不同的数据
	List prs = new ArrayList();
	for (int i = (currentPage - 1) * lineSize; i < currentPage
			* lineSize; i++) {
		if (i >= r.size())
			break;
		prs.add(r.get(i));
	}
	request.setAttribute("prs", prs);
	request.setAttribute("lineSize", lineSize);
	request.setAttribute("currentPage", currentPage);
	request.setAttribute("pageSize", pageSize);
	request.setAttribute("allRecords", allRecords);
	request.setAttribute("cate", cate);
	request.setAttribute("cp", currentPage);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>课程添加页</title>
<script src="public/jquery-1.8.3.min.js"></script>
<link href="styles/base.css" rel="stylesheet" type="text/css" />
<link href="styles/common.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
    .pop-box {   
        z-index: 9999; /*这个数值要足够大，才能够显示在最上层*/  
        margin-bottom: 3px;   
        display: none;   
        position: absolute;   
        background: #FFF;   
        border:solid 1px #6e8bde;   
    }   
    .pop-box-body {   
        clear: both;   
        margin: 4px;   
        padding: 2px;   
    } 
    .mask {   
        color:#e0e0e0;
        background-color:#e0e0e0;
        position:absolute;
        top:0px;
        left:0px;
        filter:alpha(opacity=50);  /*支持 ie 浏览器*/
        -moz-opacity:0.5; /*支持 firefox 浏览器*/
        opacity:0.5;  /*支持 chrome, opera, safari 等浏览器*/
    } 
</style>
<script type="text/javascript">
	function changeCategory(cate){
		document.mypage.cp.value = 1;
		document.mypage.cate.value = cate;
		document.mypage.submit();
	}
	function changePage(cup){
		document.mypage.cp.value = cup;
		document.mypage.submit();
	}
	function changeSelect(){
		document.mypage.cp.value = document.mypage.mySelect.value;
		document.mypage.submit();
	}
    function schCourse(){
        var kw = document.sch.k.value;
        if(kw == null || $.trim(kw).length == 0)
            return;
        window.location.href='search.jsp?k='+encodeURI(document.sch.k.value)
    }
    function addCourse(){
        var chk_value =[];    
        $('input[name="ca"]:checked').each(function(){    
            chk_value.push($(this).val());    
        });
        if(chk_value.length == 0)return;
        $.ajax({
            type: 'POST',
            url: 'opt',
            data: { 'act':'addCourse','course':chk_value.join(',')
            },
            success: function(data, textStatus, jqXHR){
                if(data.success){
                    alert('添加成功');
                    document.mypage.submit();
                }else{
                    alert('添加失败');
                }
            },
            dataType:'json'
        });
    }
	$(function() {
        $("#chk_all").click(function(){
            //console.debug($(this).attr("checked"));
            $("input[name='ca']").prop("checked",$(this).prop("checked"));
        });
    });
</script>
<script language=javascript type="text/javascript">
function popupDiv(url) {
    //console.debug($("param[name='movie']"))
    $("param[name='movie']").prop("value","http://svod.oa.sci99.com/flash/videoauto.swf?vcastr_file=/upvideo/"+url+"&IsAutoPlay=1");
    $("#pv").prop("src","http://svod.oa.sci99.com/flash/videoauto.swf?vcastr_file=/upvideo/"+url+"&IsAutoPlay=1");
    var div_obj = $("#pop-div");  
    var windowWidth = document.documentElement.clientWidth;       
    var windowHeight = document.documentElement.clientHeight;  
    var popupHeight = div_obj.height();       
    var popupWidth = div_obj.width();    
    //添加并显示遮罩层   
    $("<div id='mask'></div>").addClass("mask")   
                              .width(windowWidth + document.body.scrollWidth)   
                              .height(windowHeight + document.body.scrollHeight)   
                              .click(function() {hideDiv(); })   
                              .appendTo("body")   
                              .fadeIn(200);   
    div_obj.css({"position": "absolute"})   
           .animate({left: windowWidth/2-popupWidth/2,    
                     top: windowHeight/2-popupHeight/2, opacity: "show" }, 0);   
                    
}   
  
function hideDiv() {   
    $("#mask").remove();   
    $("#pop-div").animate({opacity: "hide" }, 100);   
}  
</script>
</head>
<body>
    <div id='pop-div' style="width: 320px;" class="pop-box">  
        <div class="pop-box-body" >  
            <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 
            codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0"
            width="308" height="246">
            <param name="movie" value="http://svod.oa.sci99.com/flash/videoauto.swf?vcastr_file=/upvideo/2012-06/201206261452288725.flv&amp;IsAutoPlay=1" />
            <param name="quality" value="high" />
            <param name="allowFullScreen" value="true" />
            <param name="IsAutoPlay" value="1" />
            <embed id="pv" src="http://svod.oa.sci99.com/flash/videoauto.swf?vcastr_file=/upvideo/2012-06/201206261452288725.flv&amp;IsAutoPlay=1"
                width="308" height="246" allowfullscreen="true" quality="high"
                pluginspage="http://www.macromedia.com/go/getflashplayer"
                type="application/x-shockwave-flash" />
            </object>
        </div>  
    </div>
	<div class="main_out">
		<div class="main_top">
			<a href=""></a> <span>人力资源培训服务热线：6091073 7071868</span>
		</div>
		<div class="clear"></div>
		<div class="nav">
			<ul class="nav_l">
				<li><a href="index.jsp">首&nbsp;&nbsp;页</a></li>
				<li><a href="manage.jsp">课程管理</a></li>
				<!--li><a href="search.jsp">课程搜索</a></li -->
				<!--li><a href="add.jsp">课程添加</a></li -->
				<!--li><a href="video.jsp">视频播放</a></li -->
				<!--li><a href="login.jsp">登录页</a></li -->
			</ul>
			<div class="nav_r">
				<a href="j_spring_security_logout">退出登录</a>
			</div>
		</div>
	</div>
				
	<div class="main"><form name="sch">
		<div class="head3 mt">
			<span>当前位置：<a href="index.jsp">卓创网络学院</a> &gt;&gt; <a
				href="manage.jsp">课程管理</a> &gt;&gt; 课程添加
			</span>
			<h2>
				<input name="k" type="text" class="searchinput" />
				<input type="button" class="buttoninput" style="cursor: pointer;"
					onclick="schCourse()" />
			</h2>
		</div></form>
		<div class="content">
			<div class="choose">
				<a href="javascript:changeCategory(3)"
					<c:if test="${cate == 3}">class="ch_ah"</c:if>>人力资源类</a> <a
					href="javascript:changeCategory(4)"
					<c:if test="${cate == 4}">class="ch_ah"</c:if>>IT类</a> <a
					href="javascript:changeCategory(6)"
					<c:if test="${cate == 6}">class="ch_ah"</c:if>>综合管理类</a> <a
					href="javascript:changeCategory(7)"
					<c:if test="${cate == 7}">class="ch_ah"</c:if>>战略管理类</a> <a
					href="javascript:changeCategory(8)"
					<c:if test="${cate == 8}">class="ch_ah"</c:if>>财务类</a> <a
					href="javascript:changeCategory(9)"
					<c:if test="${cate == 9}">class="ch_ah"</c:if>>生产类</a> <a
					href="javascript:changeCategory(10)"
					<c:if test="${cate == 10}">class="ch_ah"</c:if>>营销类</a> <a
					href="javascript:changeCategory(11)"
					<c:if test="${cate == 11}">class="ch_ah"</c:if>>财务类</a> <a
					href="javascript:changeCategory(12)"
					<c:if test="${cate == 12}">class="ch_ah"</c:if>>领导艺术类</a> <a
					href="javascript:changeCategory(13)"
					<c:if test="${cate == 13}">class="ch_ah"</c:if>>生活类</a> <a
					href="javascript:changeCategory(14)"
					<c:if test="${cate == 14}">class="ch_ah"</c:if>>客服产品知识类</a> <a
					href="javascript:changeCategory(15)"
					<c:if test="${cate == 15}">class="ch_ah"</c:if>>会议会展类</a> <a
					href="javascript:changeCategory(20)"
					<c:if test="${cate == 20}">class="ch_ah"</c:if>>MBA全景教程</a>
				<div class="clear"></div>
			</div>
			<div class="content1 mt">
				<table id="selectable" cellspacing="0" class="tab_my">
					<thead>
						<tr>
							<th width="45">[选择]</th>
							<th>[课程名称]</th>
							<th width="45">[课时]</th>
							<th width="55">[讲师]</th>
							<th width="45">&nbsp;</th>
							<th width="35">&nbsp;</th>
						</tr>
					</thead>
					<c:forEach items="${prs }" begin="0" end="${lineSize }" var="mp">
						<tr class="ui-widget-content">
							<th><input name="ca" type="checkbox" value="${mp.id}" /></th>
							<td><c:out value="${mp.name}" /></td>
							<th><c:out value="${mp.period}" /></th>
							<td><c:out value="${mp.lecturer}" /></td>
							<td class="red">&nbsp;</td>
							<td><a href="javascript:popupDiv('${mp.video_url}');"></a></td>
						</tr>
					</c:forEach>
				</table>
				<div class="add">
					<span><input id="chk_all" type="checkbox" value="" />全选</span> <a
						href="javascript:addCourse()"></a>
				</div>
				<form name="mypage" action="add.jsp" method="post" class="add">
					<input type="button" name="first" value="第一页"
						onclick="changePage(1)"
						<c:if test="${currentPage == 1}">disabled</c:if> /> <input
						type="button" name="pre" value="上一页"
						onclick="changePage(${currentPage - 1})"
						<c:if test="${currentPage == 1}">disabled</c:if> /> <input
						type="button" name="next" value="下一页"
						onclick="changePage(${currentPage + 1})"
						<c:if test="${currentPage == pageSize || allRecords == 0}">disabled</c:if> />
					<input type="button" name="last" value="最后页"
						onclick="changePage(${pageSize})"
						<c:if test="${currentPage == pageSize || allRecords == 0}">disabled</c:if> />
					<input type="hidden" name="cp" value="${cp}" />
                    <input type="hidden" name="cate" value="${cate}" />
					<c:out value="${currentPage }" />
					/
					<c:out value="${pageSize}" />
					跳转到 <select name="mySelect" onchange="changeSelect()">
						<c:forEach begin="1" end="${pageSize }" var="i">
							<option value="${i}"
								<c:if test="${currentPage == i}">selected</c:if>>
								<c:out value="${i}"></c:out>
							</option>
						</c:forEach>
					</select> 页
				</form>
			</div>
		</div>
	</div>
	<div id="rsdiv">
	</div>
	<div class="foot_out mt">
		<div class="foot">Copyright &copy; 2004-2012 SCI99 Inc. All
			rights reserved.</div>
	</div>
</body>
</html>
